package br.com.rapidrest.chain;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletResponse;

import br.com.rapidrest.annotation.ContentType;
import br.com.rapidrest.exception.ChainException;

public class ContentTypeCommand implements Command {

	@Override
	public void execute(Chain chain) throws ChainException {
		BasicChainContext context = (BasicChainContext) chain.getContext();
		
		Method method = context.getMethod();
		if (method != null && method.isAnnotationPresent(ContentType.class)) {
			ContentType annotation = method.getAnnotation(ContentType.class);
			HttpServletResponse response = context.getResponse();
			response.setContentType(annotation.value());
		}
		chain.proceed();
	}

}
